/* $Id: LocalesModel.java 129 2011-08-26 05:27:43Z altdotua@gmail.com $ */

package alt.djudge.frontend.server.models;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.criterion.Order;

import alt.djudge.frontend.server.Deployment;
import alt.djudge.frontend.server.datatypes.LocaleEntry;


public class LocalesModel
{
	private static CoreInterface<LocaleEntry> core = Deployment.isGAE() ?  
		new AbstractBigtableModelCore<LocaleEntry>()
		{
			@Override
			protected Order getOrdering()
			{
				return Order.asc("id");
			}
			
			@Override
			protected Class<LocaleEntry> getEntryClass()
			{
				return LocaleEntry.class;
			}
		}
	:
		new AbstractDBModelCore<LocaleEntry>()
		{
			@Override
			protected Order getOrdering()
			{
				return Order.asc("id");
			}
			
			@Override
			protected Class<LocaleEntry> getEntryClass()
			{
				return LocaleEntry.class;
			}
		};
	
	public static LocaleEntry getEntry(Long entryId)
	{
		return core.getEntry(entryId);
	}
		
	public static List<LocaleEntry> getAllEntries()
	{
		return core.getAllEntries();
	}
	
	public static List<LocaleEntry> getActiveEntries()
	{
		return core.getAllEntries();
	}
	
	public static List<LocaleEntry> getEntries(Map<String, Object> map)
	{
		return core.getEntries(map);
	}
	
	public static boolean deleteEntry(Long pageId)
	{
		return core.deleteEntry(pageId);
	}

	public static CoreInterface<LocaleEntry> getCore()
	{
		return core.getAdapter();
	}

	public static LocaleEntry getDefaultEntry()
	{
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("isDefault", true);
		List<LocaleEntry> locales = getEntries(params);
		return locales.size() > 0 ?  locales.get(0) : null;
	}

	public static LocaleEntry getEntryByJavaSid(String sid)
	{
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("javaLocale", sid);
		List<LocaleEntry> locales = getEntries(params);
		return locales.size() > 0 ?  locales.get(0) : null;
	}
}
